package com.skytech.component.crawler.scratch;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class DataTypeService {
    @Autowired
    private DataTypeDAO dataTypeDAO;
    @Autowired
    private RestTemplateBuilder restTemplateBuilder;
    @Value("${elasticsearch.server}")
    private String server;

    public void createIndex(JSONObject jsonObject) {


        DataType dataType=new DataType();
        dataType.setId(UUID.randomUUID().toString());
        dataType.setDataType(jsonObject.getString("type"));
        dataType.setDataDescribe(jsonObject.getString("describe"));
        if(dataTypeDAO.findByDataType(jsonObject.getString("type")).isEmpty()){
            dataTypeDAO.save(dataType);

            restTemplateBuilder.build().put(server+jsonObject.getString("type"),new JSONObject());

        }


    }

    public void createMapping(List<DataFormat> dataFormatList) {
        JSONObject indexMapping=indexMapping(dataFormatList);
        restTemplateBuilder.build().put(server+dataFormatList.get(0).getDataType()+"/news/_mapping", indexMapping,JSONObject.class);
    }

    private JSONObject indexMapping(List<DataFormat> dataFormatList) {
             JSONObject CRAWLER_NEWS=JSON.parseObject("{\n" +
                     "    \"news\": {\n" +
                     "            \"properties\": {\n" +
                     "                  \"sourceUrl\":{\n" +
                     "                    \"type\":\"text\"\n" +
                     "                  },\n" +
                     "              \"urlId\":{\n" +
                     "                    \"type\":\"keyword\"\n" +
                     "                  },\n" +
                     "              \"ruleId\":{\n" +
                     "                    \"type\":\"keyword\"\n" +
                     "                  },\n" +
                     "            \n" +
                     "              \"timestamp\":{\n" +
                     "                \"type\":\"keyword\"\n" +
                     "              },\n" +
                     "         \n" +
                     "               \"dataType\":{\n" +
                     "                \"type\":\"keyword\"\n" +
                     "              }\n" +
                     "            }\n" +
                     "        }\n" +
                     "  }");

        for (DataFormat dataFormat:dataFormatList) {
            String type=dataFormat.getDataFormat();
            JSONObject jsonObject=JSON.parseObject("{\n" +
                    "                    \"type\": \"text\"\n" +
                    "                  \n" +
                    "                }");
            CRAWLER_NEWS.getJSONObject("news").getJSONObject("properties").put(type,jsonObject);
        }


             return CRAWLER_NEWS;
    }

}
